// pages/flea/mySend/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    surveyList:[],
    pageIndex:1,
    pageSize:10,
    loading: false,
    netWork: true,
    isLoading: true,
    netWork:false,
    hasData:false,
    surveyType:0,
    roomCount:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('notice',options)
    if(options.type){
      this.data.surveyType = parseInt(options.type);
    }
    this.setData({
      surveyType: this.data.surveyType
    })
    if(this.data.surveyType == 1){
      wx.setNavigationBarTitle({
        title: '物业公投',
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initRequest()
  },
  initRequest(){
    var that = this;
    that.data.pageIndex = 1;
    that.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      surveyList:[]
    });
    that.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/survey/list/new', {
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex,
      survey_type: that.data.surveyType,
      is_my: 0,
    }, function (res) {
      if (res.data.code == 0) {

        that.setData({
          roomCount: res.data.data.roomCount,
        })




        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1;
          console.log("问卷调查列表", res)
          var records = res.data.data.surveys;
          if(records.length > 0){
            for (var index in records) {
              that.data.surveyList.splice(that.data.surveyList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              surveyList: that.data.surveyList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  questionDetail(res){
    console.log('questionDetail',res)
    var id = res.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pagesProperty/pages/survey/question/index?id='+ id,
    })
  },
  goAddRoom(){
    wx.navigateTo({
      url: '/pagesProperty/pages/feeOrder/index',
    })
  },
  goMySend(e){
    var estate_id = app.getStorageSync("estate_id");
    if(estate_id == 0){
      this.handleEstate();
    }else{
      wx.navigateTo({
        url: '/pagesProperty/pages/survey/mySend/index',
      })
    }
  },
  handleEstate(){
    var that = this;
    var message = "请绑定小区后再进行购买"
    Dialog.confirm({
      title: '绑定小区',
      message
    }).then(() => {
      wx.navigateTo({
        url: '/pages/account/index?from_type=product',
      })
    })
    .catch(() => {
      Dialog.close();
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.initRequest()
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index])
    }
    common_Js.preImg(a, c)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})